#coding=utf8
#/*
#*  
#*  彩色旋转立方体
#*  1. 定义六个面，同时定义每个顶点的颜色（与顶点坐标相对应）
#*  2. 启用 GL_SMOOTH 方式渲染
#*
#*
#*/
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

xrot = 0.0
yrot = 0.0
zrot = 0.0


def cube():
    glBegin(GL_QUADS);
    glColor3f(1.0,1.0,0.0);            
    glVertex3f( 1.0, 1.0,-1.0);        
    glColor3f(0.0,1.0,0.0);            
    glVertex3f(-1.0, 1.0,-1.0);        
    glColor3f(0.0,1.0,1.0);            
    glVertex3f(-1.0, 1.0, 1.0);        
    glColor3f(1.0,1.0,1.0);            
    glVertex3f( 1.0, 1.0, 1.0);        
                                        
    glColor3f(1.0,0.0,1.0);            
    glVertex3f( 1.0,-1.0, 1.0);        
    glColor3f(0.0,0.0,1.0);            
    glVertex3f(-1.0,-1.0, 1.0);        
    glColor3f(0.0,0.0,0.0);            
    glVertex3f(-1.0,-1.0,-1.0);        
    glColor3f(1.0,0.0,0.0);            
    glVertex3f( 1.0,-1.0,-1.0);        
                                        
    glColor3f(1.0,1.0,1.0);            
    glVertex3f( 1.0, 1.0, 1.0);        
    glColor3f(0.0,1.0,1.0);            
    glVertex3f(-1.0, 1.0, 1.0);        
    glColor3f(0.0,0.0,1.0);            
    glVertex3f(-1.0,-1.0, 1.0);        
    glColor3f(1.0,0.0,1.0);            
    glVertex3f( 1.0,-1.0, 1.0);        
                                        
    glColor3f(1.0,0.0,0.0);            
    glVertex3f( 1.0,-1.0,-1.0);        
    glColor3f(0.0,0.0,0.0);            
    glVertex3f(-1.0,-1.0,-1.0);        
    glColor3f(0.0,1.0,0.0);            
    glVertex3f(-1.0, 1.0,-1.0);        
    glColor3f(1.0,1.0,0.0);            
    glVertex3f( 1.0, 1.0,-1.0);        
                                        
    glColor3f(0.0,1.0,1.0);            
    glVertex3f(-1.0, 1.0, 1.0);        
    glColor3f(0.0,1.0,0.0);            
    glVertex3f(-1.0, 1.0,-1.0);        
    glColor3f(0.0,0.0,0.0);            
    glVertex3f(-1.0,-1.0,-1.0);        
    glColor3f(0.0,0.0,1.0);            
    glVertex3f(-1.0,-1.0, 1.0);        
                                        
    glColor3f(1.0,1.0,0.0);            
    glVertex3f( 1.0, 1.0,-1.0);        
    glColor3f(1.0,1.0,1.0);            
    glVertex3f( 1.0, 1.0, 1.0);        
    glColor3f(1.0,0.0,1.0);            
    glVertex3f( 1.0,-1.0, 1.0);        
    glColor3f(1.0,0.0,0.0);            
    glVertex3f( 1.0,-1.0,-1.0);        
    glEnd();


def display():
    global xrot,yrot,zrot
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0, 0, -5);
    glRotatef(xrot, 1, 0, 0);
    glRotatef(yrot, 0, 1, 0);
    glRotatef(zrot, 0, 0, 1);
    #glPolygonMode(GL_FRONT, GL_LINE);
    cube();
    xrot = xrot + 1;
    yrot = yrot + 1;
    zrot = zrot + 1;

    glutSwapBuffers();


def reshape(w,h):
    if h==0: 
        h = 1;
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, w/h, 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW);


def init(width,height):
    if height == 0:
       height = 1;
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearDepth(1.0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, width/height, 1, 100.0);
    glMatrixMode(GL_MODELVIEW);



def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(400, 100);
    glutInitWindowSize(640, 480);
    glutCreateWindow("");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    
    glutReshapeFunc(reshape);
    #glutKeyboardFunc(keyboard);
    init(640, 480);
    glutMainLoop();

if __name__=="__main__":
    main()
